package com.mdd.front.controller.yangyangLeadingEnterprise.personalCenter;

import com.mdd.common.aop.NotLogin;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppEnterprise;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppEnterpriseSupplyDemand;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppUser;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.*;
import com.mdd.common.util.ToolUtils;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.front.service.yangyangLeadingEnterprise.*;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppUserCreateValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppUserSearchValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppUserUpdateValidate;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppUserDetailVo;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppUserListedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/personalCenter/user")
@Api(tags = "个人中心")
public class TAppUserController {

    @Resource
    ITAppUserService iTAppUserService;

    @Resource
    ITAppEnterpriseService appEnterpriseService;

    @Resource
    ITAppEnterpriseSupplyDemandService appEnterpriseSupplyDemandService;

    @Resource
    ITAppEnterpriseUserService appEnterpriseUserService;

    @Resource
    ITAppProblemFeedbackService appProblemFeedbackService;

    /**
     * 查询个人中心的用户信息（实时）
     * @param param
     * @return
     */
    @NotLogin
    @PostMapping(value = "/selectUserForPersonalCenter",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="查询个人中心的用户信息（实时）")
    public String selectUserForPersonalCenter(@RequestBody CommonParam param){
        return iTAppUserService.selectUserForPersonalCenter(param.getUserId());
    }

    /**
     * 根据id查询用户信息
     * @param param
     * @return
     */
    @NotLogin
    @PostMapping(value = "/selectById",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="根据id查询用户信息")
    public String selectById(@RequestBody CommonParam param) {
        Integer userId = param.getUserId();
        if(userId==null || userId==0){
            return JsonReturn.fail("必传参数不能为空",40001).toJsonStr();
        }
        TAppUser appUser = iTAppUserService.selectById(userId);
        if(appUser==null || appUser.getId()==null){
            return JsonReturn.fail("用户数据不存在",40002).toJsonStr();
        }
        return JsonReturn.success().put("appUser",appUser).toJsonStr();
    }

    /**
     * 修改用户信息
     * @param param
     * @return
     */
    @NotLogin
    @PostMapping(value = "/updateById",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="修改用户信息")
    public String updateById(@RequestBody AppUserParam param) {
        Long userId = param.getId();
        if (userId==null || userId==0){
            return JsonReturn.fail("必传参数不能为空",40001).toJsonStr();
        }
        TAppUser appUser = new TAppUser();
        appUser.setId(userId);
        //个人介绍
        String personalIntroduction = param.getPersonalIntroduction();
        if(personalIntroduction!=null && !personalIntroduction.isEmpty()){
            appUser.setPersonalIntroduction(personalIntroduction);
        }
        //手机号
        String phone = param.getPhone();
        if(phone!=null && !phone.isEmpty()){
            appUser.setPhone(phone);
        }
        //昵称
        String nickname = param.getNickname();
        if(nickname!=null && !nickname.isEmpty()){
            appUser.setNickname(nickname);
        }
        //个人头像
        String personalPic = param.getPersonalPic();
        if(personalPic!=null && !personalPic.isEmpty()){
            appUser.setPersonalPic(personalPic);
        }
        //背景图
        String backgroundPic = param.getBackgroundPic();
        if(backgroundPic!=null && !backgroundPic.isEmpty()){
            appUser.setBackgroundPic(backgroundPic);
        }
        //密码
        String password = param.getPassword();
        if(password!=null && !password.isEmpty()){
            String pwdParam  = ToolUtils.makePassword(password);
            appUser.setPassword(pwdParam);
        }
        return iTAppUserService.updateById(appUser);
    }

    /**
     * 增加用户访客数量
     * @param userId
     * @return
     */
    @NotLogin
    @ApiIgnore
    @PostMapping(value = "/updateAppUserVisitorNumById",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="增加用户访客数量")
    public String updateAppUserVisitorNumById(@RequestBody Integer userId) {
        return iTAppUserService.updateAppUserVisitorNumById(userId);
    }

    /**
     * 用户绑定新的企业信息
     * @param appEnterprise
     * @return
     */
    @NotLogin
    @PostMapping(value = "/insertEnterpriseByUserId",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="用户绑定新的企业信息")
    public String insertEnterpriseByUserId(@RequestBody TAppEnterprise appEnterprise) {
        return appEnterpriseService.insertEnterpriseByUserId(appEnterprise);
    }

    /**
     * 用户绑定新的企业信息 点击下一步
     * @param param
     * @return
     */
    @NotLogin
    @PostMapping(value = "/insertEnterpriseUser",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="用户绑定新的企业信息---点击下一步")
    public String insertEnterpriseUser(@Validated @RequestBody EnterpriseUserParam param) {
        return appEnterpriseUserService.insertEnterpriseUser(param);
    }

    /**
     * 查询用户指定企业下的我得供应/我得需求
     * @param param
     * @return
     */
    @NotLogin
    @PostMapping(value = "/selectEnterpriseSupplyDemandList",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="查询用户指定企业下的我得供应/我得需求")
    public String selectEnterpriseSupplyDemandList(@RequestBody EnterpriseSupplyDemandParam param) {
        return appEnterpriseSupplyDemandService.selectEnterpriseSupplyDemandList(param);
    }

    /**
     * 根据企业供需id查询企业供需（用于分享链接跳转页面之后进行加载数据）
     * @param enterpriseSupplyDemandId
     * @return
     */
    @NotLogin
    @PostMapping(value = "/selectEnterpriseSupplyDemandById",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="根据企业供需id查询企业供需（用于分享链接跳转页面之后进行加载数据）")
    public String selectEnterpriseSupplyDemandById(@RequestBody Integer enterpriseSupplyDemandId) {
        return appEnterpriseSupplyDemandService.selectEnterpriseSupplyDemandById(enterpriseSupplyDemandId);
    }

    /**
     * 企业发布新的供需
     * @param appEnterpriseSupplyDemand
     * @return
     */
    @NotLogin
    @PostMapping(value = "/insertAppEnterpriseSupplyDemand",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="企业发布新的供需")
    public String insertAppEnterpriseSupplyDemand(@RequestBody TAppEnterpriseSupplyDemand appEnterpriseSupplyDemand) {
        return appEnterpriseSupplyDemandService.insertAppEnterpriseSupplyDemand(appEnterpriseSupplyDemand);
    }

    /**
     * 问题反馈
     * @param param
     * @return
     */
    @NotLogin
    @PostMapping(value = "/insertProblemFeedback",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="问题反馈")
    public String insertProblemFeedback(@RequestBody ProblemFeedbackParam param) {
        return appProblemFeedbackService.insertProblemFeedback(param);
    }





}
